import { RequestHandler } from 'express'
import { failure, success } from '@/utils/response'
import orderService from '@/services/orderService'

const getOrders: RequestHandler = async (req, res) => {
  try {
    const data = await orderService.getOrders(req.userId as number, req.query)
    success(res, '查询订单列表成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询订单列表失败：')
  }
}

const getOrderInfo: RequestHandler = async (req, res) => {
  try {
    const data = await orderService.getOrderInfo(req.userId as number, req.params.outTradeNo)
    success(res, '查询订单成功。', data)
  } catch (error) {
    failure(res, error as Error, '查询订单失败：')
  }
}

const addOrder: RequestHandler = async (req, res) => {
  try {
    const data = await orderService.addOrder(req.userId as number, req.body)
    success(res, '订单创建成功。', data)
  } catch (error) {
    failure(res, error as Error, '订单创建失败：')
  }
}

export default {
  getOrders,
  getOrderInfo,
  addOrder,
}
